import { Module } from '@nestjs/common';
import { APP_GUARD } from '@nestjs/core';
import { GraphQLModule } from '@nestjs/graphql';
import { join } from 'path';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthGuard } from './common/auth/auth.guard';
import { MongooseModule } from '@nestjs/mongoose';
import { UserModule } from './user/user.module';
import { QuestionModule } from './question/question.module';
import { ProblemModule } from './problem/problem.module';
import { CategoryModule } from './category/category.module';
import { AnswerModule } from './answer/answer.module';

@Module({
  imports: [
    MongooseModule.forRoot('mongodb://localhost/code', {
      useUnifiedTopology: true,
      autoIndex: true,
      useFindAndModify: false,
      useCreateIndex: true,
    }),
    GraphQLModule.forRoot({
      autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
      installSubscriptionHandlers: true,
      context: ({ req, connection }) => {
        return connection ? { req: connection.context } : { req };
      },
    }),
    UserModule,
    QuestionModule,
    ProblemModule,
    CategoryModule,
    AnswerModule,
  ],
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: APP_GUARD,
      useClass: AuthGuard,
    },
  ],
})
export class AppModule {}
